#include <iostream>
#include <math.h>

using namespace std;

short floattofixed(float float_val, short int_length, short frac_length)
{   //use two complement
    short result=0;

    if(float_val>0) //positif
    {
        result = ceil(float_val*pow(2.0,frac_length));
    }
    if(float_val<0) //negatif
    {
        result = pow(2.0,int_length+frac_length+1)+ceil(float_val*pow(2.0,frac_length));
    }

    return result;
}

float fixedtofloat(short fix_val, short int_length, short frac_length)
{
    float result=0;
    bool sign_bit;

    //sign_bit = (fix_val & (1<<(int_length+frac_length))) ? 1:0;

    if((fix_val & (1<<(int_length+frac_length)))) //negatif
    {
        result = (fix_val - pow(2.0,int_length+frac_length+1))/pow(2.0,frac_length);
    }
	else //positif
    {
        result = fix_val/pow(2.0,frac_length);
    }


    return result;
}

short floattofixed2(float float_val, short int_length, short frac_length)
{
    short temp=0;
    short result;

    temp=ceil(float_val);
    temp=~temp;
    result = temp+0x01;

    return result;
}

long fixadd()
{
	long result;

	return result;
}

int main()
{
    cout << "Hello world!" << endl;

    short a,b;
    float c;

    a=0x8000;
    b=floattofixed(0.999969482421875, 0, 15);
    cout<<"floattofixed="<<b<<endl;
//    b=floattofixed2(1, 0, 15);
//    cout<<"floattofixed2="<<b<<endl;
    c=fixedtofloat(1, 0, 15);
    cout<<"fixedtofloat="<<c<<endl;
//    cout<<"c="<<(138&(32>>2))<<endl;
//
//    bool tes;
//
//    tes = twocomplement(a);
//    cout<<"tes="<<tes<<endl;
//    cout<<"sign="<<(0x8000&(1<<15))<<endl;
//    cout<<"two="<<(pow(2,16))<<endl;
    return 0;
}
